<script setup>
import {ref} from "vue"
import {channelGetService, channelDelService} from "@/api/channel"
import {Delete, Edit} from "@element-plus/icons-vue";
import ChannelEdit from "@/components/ChannelEdit.vue";

// 表格loading效果
const loading = ref(false)

// 绑定子组件中的dialog会话框组件
const dialog = ref()

// 获取文章分类
const channelList = ref([])
const getChannelList = async () => {
  loading.value = true
  const res = await channelGetService()
  channelList.value = res.data
  loading.value = false
}
getChannelList()

// 添加文章分类
const onAddChannel = () => {
  dialog.value.open({})
}

// 编辑文章分类
const onEditChannel = (row) => {
  dialog.value.open(row)

}

// 删除文章分类
const onDeleteChannel = async (row) => {
  await ElMessageBox.confirm(
    '你确认删除该分类信息吗？',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  await channelDelService(row.id)
  ElMessage.success('删除成功')
  getChannelList()
}

// 重新刷新列表
const onSuccess = () => {
  getChannelList()
}

</script>

<template>
  <PageContainer title="文章分类">
    <template #extra>
      <el-button @click="onAddChannel" type="primary">添加分类</el-button>
    </template>

    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"/>
      <el-table-column prop="cate_name" label="分类名称"/>
      <el-table-column prop="cate_alias" label="分类别名"/>
      <el-table-column label="操作" width="100">
        <template #default="{row}">
          <el-button @click="onEditChannel(row)" type="primary" :icon="Edit" plain
                     circle></el-button>
          <el-button @click="onDeleteChannel(row)" type="danger" :icon="Delete" plain
                     circle></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>

    <channel-edit @success="onSuccess" ref="dialog"></channel-edit>
  </PageContainer>
</template>
